package com.taotao.manager.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.manager.service.ContentCategoryService;
import com.taotao.pojo.TbContentCategory;

@RequestMapping("content/category")
@Controller
public class ContentCategoryController {

	private static final Logger LOGGER = LoggerFactory.getLogger(ContentCategoryController.class);
	
	@Autowired
	private ContentCategoryService categoryService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<List<TbContentCategory>> queryListByParentId(
			@RequestParam(value = "id", defaultValue = "0") long pid) {
		try {
			TbContentCategory category = new TbContentCategory();
			category.setParentId(pid);
			List<TbContentCategory> result = this.categoryService.queryListByWhere(category);
			if(result == null || result.isEmpty()) {
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			return ResponseEntity.ok(result);
		} catch (Exception e) {
			LOGGER.error("查询内容分类出错！",e);
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
	/**
	 * 保存内容类目，保存完之后看父节点属性是否为true，如果不是则修改为true
	 * <p>Title: saveContentCategory</p>
	 * <p>Description: </p>
	 * @param contentCategory
	 * @return
	 */
	  @RequestMapping(method = RequestMethod.POST)
	  public ResponseEntity<TbContentCategory> saveContentCategory(TbContentCategory contentCategory) {
	      try {
	          this.categoryService.saveContentCategory(contentCategory);
	          return ResponseEntity.status(HttpStatus.CREATED).body(contentCategory);
	      } catch (Exception e) {
	         LOGGER.error("保存内容类目失败",e);
	      }
	      return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	  }
	    
	    /**
	     * 重命名
	     * 
	     * @param contentCategory
	     * @return
	     */
	    @RequestMapping(method = RequestMethod.PUT)
	    public ResponseEntity<Void> rename(TbContentCategory contentCategory) {
	        try {
	            this.categoryService.updateContentCategory(contentCategory);
	            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
	        } catch (Exception e) {
	           LOGGER.error("重命名失败",e);
	        }
	        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	    }

	    /**
	     * 删除节点
	     * 
	     * @param contentCategory
	     * @return
	     */
	    @RequestMapping(method = RequestMethod.DELETE)
	    public ResponseEntity<Void> delete(TbContentCategory contentCategory) {
	        try {
	            this.categoryService.deleteContentCategory(contentCategory);
	            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	    }
}
